﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using netDxf;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Tables;

namespace TestDxfDocument
{
    class PAW2
    {
        public static void Standard17645_p44(Dictionary<string, Object> data)
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层
                                                                           //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            var d_in = 10.0;
            try
            {
                d_in = Convert.ToDouble(data["din"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[d_in] not found");
            }
            var d_out = 15.0;
            try
            {
                d_out = Convert.ToDouble(data["dout"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[d_out] not found");
            }
            var E = 1.0;
            try
            {
                E = Convert.ToDouble(data["e"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[E] not found");
            }

            //坐标
            var v0 = new Vector2(0, 0);
            var v1 = fun.Polar(v0, pi, 1.5 * d_out);
            var v2 = fun.Polar(v1, 90 * pi / 180, d_in / 2);
            var v3 = fun.Polar(v1, 90 * pi / 180, d_out / 2);
            var v4 = fun.Polar(v3, pi, E);
            var v5 = fun.Polar(v2, pi, E);
            var v6 = fun.Polar(v1, pi, E);
            var v7 = fun.mirrorpoint(v2, v1, v6);
            var v8 = fun.mirrorpoint(v3, v1, v6);
            var v9 = fun.mirrorpoint(v4, v1, v6);
            var v10 = fun.mirrorpoint(v5, v1, v6);
            var v11 = fun.mirrorpoint(v2, v0, fun.Polar(v0, 90 * pi / 180, 1));
            var v12 = fun.mirrorpoint(v3, v0, fun.Polar(v0, 90 * pi / 180, 1));
            var v13 = fun.mirrorpoint(v4, v0, fun.Polar(v0, 90 * pi / 180, 1));
            var v14 = fun.mirrorpoint(v5, v0, fun.Polar(v0, 90 * pi / 180, 1));
            var v15 = fun.mirrorpoint(v7, v0, fun.Polar(v0, 90 * pi / 180, 1));
            var v16 = fun.mirrorpoint(v8, v0, fun.Polar(v0, 90 * pi / 180, 1));
            var v17 = fun.mirrorpoint(v9, v0, fun.Polar(v0, 90 * pi / 180, 1));
            var v18 = fun.mirrorpoint(v10, v0, fun.Polar(v0, 90 * pi / 180, 1));
            var v19 = fun.Polar(v0, -90 * pi / 180, 3 * d_out);

            var polyline1 = Pus.AddPointsToLwPolyLine(v3, v4, v9, v8, v3);
            fun.PolyFill(dxf, polyline1, 0.2, contour);
            var polyline4 = Pus.AddPointsToLwPolyLine(v2, v5, v10, v7, v3);
            polyline4.Lineweight = Lineweight.W25;
            polyline4.Layer = additionalline;
            dxf.AddEntity(polyline4);

            var polyline2 = Pus.AddPointsToLwPolyLine(fun.Polar(v0, pi, d_out / 2), fun.Polar(v0, 0, d_out / 2), fun.Polar(v0, pi, d_out / 2));
            polyline2.Vertexes[0].Bulge = Math.Tan(180 * Math.PI / 180 / 4);
            polyline2.Vertexes[1].Bulge = Math.Tan(180 * Math.PI / 180 / 4);
            fun.PolyFill(dxf, polyline2, 0.2, contour);
            var polyline3 = Pus.AddPointsToLwPolyLine(fun.Polar(v0, pi, d_in / 2), fun.Polar(v0, 0, d_in / 2), fun.Polar(v0, pi, d_in / 2));
            polyline3.Vertexes[0].Bulge = Math.Tan(180 * Math.PI / 180 / 4);
            polyline3.Vertexes[1].Bulge = Math.Tan(180 * Math.PI / 180 / 4);
            fun.PolyFill(dxf, polyline3, 0.2, contour);







            var height = 1.5;//字体高度
            var offset = 10;//偏移量
            var arrowsize = 0.5;//箭头大小
            var exline = 0.5;//延伸线长度

            fun.dimrot1(dxf, v3, v4, 0, d_out * 1 / 6, 0, height, arrowsize, exline, "e =" + E, "", "");
            fun.dimrot1(dxf, fun.Polar(v0, pi, d_out / 2), fun.Polar(v0, 0, d_out / 2), 0, d_out * 2 / 3, 0, height, arrowsize, exline, "d_dou=" + d_out, "", "");
            fun.dimrot1(dxf, fun.Polar(v0, pi, d_in / 2), fun.Polar(v0, 0, d_in / 2), 0, 0, 0, height, arrowsize, exline, "d_in=" + d_in, "", "");

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            double scale = Convert.ToDouble(data["scale"]); //放大系数
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }



            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
